<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>方案维护页面</title>
    <script type="text/javascript" src="../../res/js/jquery-3.4.1.min.js"></script>
    <script src="../../layui/layui.all.js"></script>
    <script type="text/javascript" src="../../js/Tools.js"></script>
    <link rel="stylesheet" type="text/css" href="../../layui/css/layui.css" />
    <style>
        body{
            padding: 10px 20px 10px 10px;
        }

    </style>

    <script>
        $.ajaxSetup({
            async: false
        });
    </script>
</head>
<body>
<form class="layui-form" id="atsSchemeForm">
    <input type="hidden" name="schemeId" id="schemeId" lay-verify="schemeId" autocomplete="off" class="layui-input">
    <input type="hidden" name="schemeType" id="schemeType" lay-verify="schemeType" autocomplete="off" class="layui-input">
    <input type="hidden" name="fileUrls" id="fileUrls" lay-verify="fileUrls" autocomplete="off" class="layui-input">
    <input type="hidden" name="resultFile" id="resultFile" lay-verify="resultFile" autocomplete="off" class="layui-input">
    <input type="hidden" name="remark" id="remark" lay-verify="remark" autocomplete="off" class="layui-input">
    <div class="layui-form-item">
        <label class="layui-form-label">方案名称</label>
        <div class="layui-input-block">
            <input type="text" name="schemeName" id="schemeName" lay-verify="schemeName" autocomplete="off" placeholder="请输入方案名称" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">运动员</label>
        <div class="layui-input-block" id="yundongyuan">

        </div>
    </div>
    <label class="layui-form-label">附件</label>
    <div class="layui-upload" id="fileUpload">
        <button type="button" class="layui-btn layui-btn-normal" id="testList">选择多文件</button>

        <div class="layui-upload-list">

            <table class="layui-table">
                <thead>
                <tr><th>文件名</th>
                    <th>大小</th>
                    <th>状态</th>
                    <th>操作</th>
                </tr></thead>
                <tbody id="demoList"></tbody>
            </table>
        </div>
        <label class="layui-form-label">                 </label>
        <button type="button" class="layui-btn" id="testListAction">开始上传文件</button>
    </div>
    <label class="layui-form-label"></label>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <input type="button" class="layui-btn" lay-submit="" lay-filter="demo1" id="subBtn" value="立即提交"/>
            <input type="reset" class="layui-btn layui-btn-primary" value="重置"/>
        </div>
    </div>
</form>

<script>
    //取到上一个页面传过来的id

    var schemeId = getQueryString("schemeId");
    var remark=getQueryString("remark");
    var schemeType = getQueryString("schemeType");
    $("#remark").val(remark);
    $("#schemeType").val(schemeType);
    var str="";
    layui.use(['form', 'layedit', 'laydate','tree','element','upload'], function () {
        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate
            , tree = layui.tree
            , element = layui.element
            , upload = layui.upload;
        var i=0;
        //多文件列表示例
        var demoListView = $('#demoList')
            , uploadListIns = upload.render({
            elem: '#testList'
            , url: getRootPath() + 'atsSchemeAction/uploadFile'
            , accept: 'file'
            , multiple: true
            , number: 3
            , auto: false
            , bindAction: '#testListAction'
            , choose: function (obj) {
                var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列

                //读取本地文件
                obj.preview(function (index, file, result) {
                    var tr = $(['<tr id="upload-' + index + '">'
                        , '<td>' + file.name + '</td>'
                        , '<td>' + (file.size / 1014).toFixed(1) + 'kb</td>'
                        , '<td>等待上传</td>'
                        , '<td>'
                        , '<button class="layui-btn layui-btn-xs demo-reload layui-hide">重传</button>'
                        , '<button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">删除</button>'
                        , '</td>'
                        , '</tr>'].join(''));

                    //单个重传
                    tr.find('.demo-reload').on('click', function () {
                        obj.upload(index, file);
                    });

                    //删除
                    tr.find('.demo-delete').on('click', function () {
                        delete files[index]; //删除对应的文件
                        tr.remove();
                        uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值，以免删除后出现同名文件不可选
                    });
                    if(i==0){
                        $('#demoList').html("");
                    }
                    demoListView.append(tr);
                    i++;
                });
            }
            , done: function (res, index, upload) {
                if (res.code == 0) { //上传成功
                    var tr = demoListView.find('tr#upload-' + index)
                        , tds = tr.children();
                    tds.eq(2).html('<span style="color: #5FB878;">上传成功</span>');
                    tds.eq(3).html(''); //清空操作
                    str+=res.msg+",";

                    //console.info(str);
                    delete this.files[index]; //删除文件队列已经上传成功的文件
                    $("#fileUrls").val(str);
                    return;
                }

                this.error(index, upload);
            }
            , error: function (index, upload, msg) {
                var tr = demoListView.find('tr#upload-' + index)
                    , tds = tr.children();
                tds.eq(2).html('<span style="color: #FF5722;">上传失败</span>');
                tds.eq(3).find('.demo-reload').removeClass('layui-hide'); //显示重传
            }
        });


        //指定允许上传的文件类型
        upload.render({
            elem: '#test3'
            ,url: getRootPath() + 'atsSchemeAction/uploadFile'
            ,accept: 'file' //普通文件
            ,done: function(res){
                $("#resultFile").val(res.msg);
            }
        });

        //日期
        laydate.render({
            elem: '#date'
        });
        laydate.render({
            elem: '#date1'
        });

        //查询运动员
        var url = getRootPath() + "atsSchemeAction/selectAthlete";
        var data = null;
        $.post(url,data,function (obj) {
            $.each(obj,function(index,item){
                $("#yundongyuan").append("<input type='checkbox' name='yundong' id='"+item.athleteId+"' value='"+item.athleteId+"' title='"+item.athleteName+"'/>");

                if(schemeId!=null&&schemeId!=""){
                    var urlType=getRootPath() + "atsSchemeAction/selectCheckAthlete";
                    var dateType={schemeId:schemeId};
                    $.post(urlType,dateType,function(objType){
                        $.each(objType,function(indexs,items){
                            if(items.athleteId==item.athleteId){
                                $("#"+item.athleteId+"").attr("checked","");
                            }
                        });
                        form.render();
                    },"json");
                }
            });
            form.render();
        },"json");

        if (schemeId) {
            var strs="";
            var url = getRootPath() + "atsSchemeAction/selectById";
            var data = {schemeId: schemeId};
            $.post(url, data, function (obj) {
                $("#schemeId").val(obj.schemeId);
                $("#schemeName").val(obj.schemeName);
                $("#schemeType").val(obj.schemeType);
                $("#remark").val(obj.remark);
                $("#sort").val(obj.sort);

                if(obj.file1){
                    strs+='<tr><td>' + obj.file1+'</td><td></td><td></td><td></td></tr>';
                }
                if(obj.file2){
                    strs+='<tr><td>' + obj.file2+'</td><td></td><td></td><td></td></tr>';
                }
                if(obj.file3){
                    strs+='<tr><td>' + obj.file3+'</td><td></td><td></td><td></td></tr>';
                }
                $("#demoList").append(strs);

            }, "json");
        };

        //自定义验证规则
        form.verify({
            schemeName: function(value){
                if(!value.length){
                    return '方案名称不能包含空格';
                }
            }
        });

        //监听提交
        form.on('submit(demo1)', function (data) {
            var url = getRootPath() + "atsSchemeAction/addOrUpdate";
            var pasChecked=$("input[name='yundong']:checked");
            console.info(pasChecked.length);
            //所有选中的复选框
            var str="";
            for(var i=0;i<pasChecked.length;i++){
                var items=pasChecked[i].value;
                //console.info(items);
                str+=items+",";
                console.info(str);
            }
            data.field.coachName=str;

            $.ajax({
                url: url,
                type: "post",
                data:data.field,
                dataType: "json",
                success: function (obj) {
                    if (obj.num == 1) {
                        layer.msg(obj.msg, {
                            icon: 1,
                            time: 1000,
                        }, function () {
                            window.parent.location.reload();//刷新父页面
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                        });
                    }
                }, error: function (e) {
                    alert('失败了');
                }

            });
        });
    });

</script>

</body>
</html>